package com.qf.java2201.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.qf.java2201.common.PageVo;
import com.qf.java2201.common.R;
import com.qf.java2201.pojo.Kind;
import com.qf.java2201.service.KindService;
import com.qf.java2201.utils.OssUtil;
import com.qf.java2201.vo.BarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author wxf
 * @Date 2022/4/26 16:34
 **/
@RestController
@RequestMapping("/flowerKind")
//@CrossOrigin(origins = {"http://127.0.0.1:5500"}, allowCredentials = "true")
public class KindController {

    @Autowired
    private KindService kindService;

    @RequestMapping("/flowerKindPageDate")
    public PageVo<Kind> flowerKindPageDate(@RequestParam Map<String, Object> params) {

        return kindService.getPageData(params);
    }

    @RequestMapping("/flowerKindAdd")
    public R flowerKindAdd(Kind kind) {
        int result = kindService.addFlowerKind(kind);

        R<Kind> r = new R<>();

        if (result == 1) {
            r.setCode(0);
            r.setMsg("添加成功");
        } else {
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }

    @RequestMapping("/deleteFlower")
    public R deleteFlower(int id) {
        int result = kindService.deleteFlower(id);

        R<Kind> r = new R<>();

        if (result == 1) {
            r.setCode(0);
            r.setMsg("删除成功");
        } else {
            r.setCode(-1);
            r.setMsg("删除失败");
        }
        return r;
    }

    @RequestMapping("/updateFlower")
    public R updateFlower(Kind kind) {
        int result = kindService.updateFlower(kind);

        R<Kind> r = new R<>();

        if (result == 1) {
            r.setCode(0);
            r.setMsg("修改成功");
        } else {
            r.setCode(-1);
            r.setMsg("修改失败");
        }
        return r;
    }


    @RequestMapping("/flowerPageData")
    public R flowerPageData(Kind kind) {
        List<Kind> result = kindService.flowerPageData(kind);

        R r = new R();

        if (result != null) {
            r.setCode(0);
            r.setData(result);
            r.setMsg("查询成功");
        } else {
            r.setCode(-1);
            r.setMsg("查询失败");
        }
        return r;
    }


    //上传图片
    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();  // 得到文件的名字

        InputStream inputStream = file.getInputStream();  // 将file 对象转为 输入流

        // 阿里云存储  返回 上传之后的文件路径
        String path = OssUtil.uploadFile(inputStream, filename);

        R<String> r = new R();
        r.setCode(0);
        r.setMsg("上传成功");
        r.setData(path);
        return r;
    }

    @RequestMapping("/echartsBar")
    public R echartsBar(){
        List<BarVo> list = kindService.dateBarData();
        System.out.println(list);

        R ok = R.ok();
        ok.setData(list);
        return ok;

    }


    @RequestMapping("/export")
    public void export(Kind kind, HttpServletResponse response) throws IOException {
        // 导出学生数据
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String filename = URLEncoder.encode("种类息导出", "utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream,Kind.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
//        PageInfo<Kind> pageData = kindService.getPageData(1, new Kind());
        List<Kind> kinds = kindService.selectList(kind);

        sheet.doWrite(kinds);
    }



    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 下载  模板
        // 设置响应数据的类型
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        // 读取服务器的模板文件 转为 字节数组
        String path = request.getServletContext().getRealPath("/template/分类信息模板.xlsx");
        File file = new File(path);
        //把文件对象 转为 字节数组对象
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //防止中文乱码
        String filename = URLEncoder.encode("分类信息导出表", "utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);

        outputStream.close();
    }


    //多条数据删除
    @RequestMapping("/deleteBySelect")
    public R deleteBySelect(String strIds) {
        String[] ids = strIds.split(",");

        ArrayList<Integer> list = new ArrayList<>();

        for (String itemId : ids) {
            int id = Integer.parseInt(itemId);

            list.add(id);
        }

        int result = kindService.deleteByIds(list);

        R r = null;

        if (result == ids.length) {
            r = new R();
            r.setCode(0);
            r.setMsg("批量删除成功");
        } else {
            r = new R();
            r.setCode(-1);
            r.setMsg("批量删除失败");
        }

        return r;
    }

}
